#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <asm/uaccess.h>


MODULE_LICENSE("GPL");
MODULE_AUTHOR("Ryan Welton");
MODULE_DESCRIPTION("CSAW CTF Challenge Kernel Module");

static struct proc_dir_entry * kstack_proc;

#define ENTRY_NAME "kstack_proc"

int
kstack_proc_write(struct file *file, const char __user *ubuf, unsigned long count, void *data)
{
    int i;
    char big_array[count];

    printk("Stacksize is %x\n", THREAD_SIZE);
    printk("Allocating %x chars\n", (unsigned int) count);

    for (i = 0; i < count; count++) {
        big_array[i] = ubuf[i];
    }

    printk("big array %i, %c\n", 0, big_array[0]);
    return count;
}

static int __init
kstack_proc_init(void)
{
    printk(KERN_INFO "Kstack proc loading up\n");
    kstack_proc = create_proc_entry(ENTRY_NAME, 0666, NULL);
    kstack_proc->write_proc = kstack_proc_write;

    return 0;
}

static void __exit
kstack_proc_exit(void)
{
    if (kstack_proc) {
        remove_proc_entry(ENTRY_NAME, kstack_proc);
    }

    printk(KERN_INFO "kstack_proc: unloading module\n");
}

module_init(kstack_proc_init);
module_exit(kstack_proc_exit);
